package com.gnerv.battle.boot.framework.core.model;

import java.util.HashMap;

/**
 * @author Gnerv LiGen
 */
public class RestResult<T> extends HashMap<String, Object> {

    private static final String CODE = "code";
    private static final String MESSAGE = "message";
    private static final String DATA = "data";

    private static final String CURRENT = "current";
    private static final String SIZE = "size";
    private static final String TOTAL = "total";

    private RestResult() {
    }

    public static <T> RestResult<T> ok() {
        return build(RestResultCode.CODE_200);
    }

    public static <T> RestResult<T> ok(T data) {
        return build(RestResultCode.CODE_200, data);
    }

    public static <T> RestResult<T> ok(RestResultCode restResultCode) {
        return build(restResultCode);
    }

    public static <T> RestResult<T> ok(RestResultCode restResultCode, T data) {
        return build(restResultCode, data);
    }

    public static <T> RestResult<T> error() {
        return build(RestResultCode.CODE_500);
    }

    public static <T> RestResult<T> error(T data) {
        return build(RestResultCode.CODE_500, data);
    }

    public static <T> RestResult<T> error(RestResultCode restResultCode) {
        return build(restResultCode);
    }

    public static <T> RestResult<T> error(RestResultCode restResultCode, T data) {
        return build(restResultCode, data);
    }

    public RestResult<T> page(Integer current, Integer size, Long total) {
        this.put(CURRENT, current);
        this.put(SIZE, size);
        this.put(TOTAL, total);
        return this;
    }

    private static <T> RestResult<T> build(RestResultCode restResultCode) {
        return build(restResultCode, null);
    }

    private static <T> RestResult<T> build(RestResultCode restResultCode, T data) {
        RestResult<T> result = new RestResult<>();
        result.put(CODE, restResultCode.getKey());
        result.put(MESSAGE, restResultCode.getDescription());
        result.put(DATA, data);
        return result;
    }

}
